package top.drinyeit.nongmao.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.math.BigDecimal;

import top.drinyeit.nongmao.R;
import top.drinyeit.nongmao.activity.MainActivity;
import top.drinyeit.nongmao.activity.MyOrderActivity;
import top.drinyeit.nongmao.base.IBaseView;
import top.drinyeit.nongmao.presenter.PayDialogPresenter;

/**
 * Created by bigHead on 2017/11/15.
 */

public class PayDialog extends Dialog implements IBaseView {

    public Activity activity;
    //    public Context context;      // 上下文
    private int layoutResID;      // 布局文件id
    private int[] listenedItems;  // 要监听的控件id
    public TextView order_price;
    public LinearLayout balance, vx, zfb;
    public CheckBox balance_box, vx_box, zfb_box;
    public FrameLayout close;
    private TextView pay, show, tv_other;
    private RelativeLayout rl_bg;
    int type = 0;
    private PayDialogPresenter payDialogPresenter;
    double orderPrice;
    String orderId;

//    public PayDialog(Context context,double orderPrice, String orderId) {
//        super(context, R.style.dialog_bottom_custom); //dialog的样式
//        this.context = context;
//        this.listenedItems = listenedItems;
//        this.orderPrice = orderPrice;
//        this.orderId = orderId;
//    }

    public PayDialog(Activity activity, double orderPrice, String orderId) {
        super(activity, R.style.dialog_bottom_custom); //dialog的样式
        this.activity = activity;
        this.listenedItems = listenedItems;
        this.orderPrice = orderPrice;
        this.orderId = orderId;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM); // 此处可以设置dialog显示的位置为底部
        window.setWindowAnimations(R.style.bottom_menu_animation); // 添加动画效果
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_pay);
        WindowManager windowManager = (activity).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = display.getWidth(); // 设置dialog宽度
        getWindow().setAttributes(lp);
//        setCanceledOnTouchOutside(true);// 点击Dialog外部消失

        initView();
        initData();
        initListener();

    }

    private void initView() {
        order_price = findViewById(R.id.order_price);
        balance = findViewById(R.id.balance);
        vx = findViewById(R.id.vx);
        zfb = findViewById(R.id.zfb);
        balance_box = findViewById(R.id.balance_box);
        vx_box = findViewById(R.id.vx_box);
        zfb_box = findViewById(R.id.zfb_box);
        close = findViewById(R.id.close);
        pay = findViewById(R.id.pay);
        show = findViewById(R.id.show);
        rl_bg = findViewById(R.id.rl_bg);
        tv_other = findViewById(R.id.tv_other);

    }

    private void initData() {
        payDialogPresenter = new PayDialogPresenter(this);
    }

    private void initListener() {
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss(activity);
            }
        });
        balance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vx_box.setChecked(false);
                zfb_box.setChecked(false);
                balance_box.setChecked(true);
                chose();
            }
        });
        vx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                balance_box.setChecked(false);
                zfb_box.setChecked(false);
                vx_box.setChecked(true);
                chose();
            }
        });
        zfb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                balance_box.setChecked(false);
                vx_box.setChecked(false);
                zfb_box.setChecked(true);
                chose();
            }
        });
        pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                payWay();
            }
        });

        rl_bg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss(activity);
            }
        });

    }

    private void payWay() {
        if (type == 0) {
            showToastMsg("请选择支付方式");
            return;
        }
        switch (type) {
            case 1:
                payDialogPresenter.zfbPayment(BigDecimal.valueOf(orderPrice), orderId);
                break;
            case 2:
                payDialogPresenter.vxPayment(BigDecimal.valueOf(orderPrice), orderId);
                break;
            default:
                payDialogPresenter.balancePayment(BigDecimal.valueOf(orderPrice), orderId);
                break;
        }
    }


    public void chose() {
        if (balance_box.isChecked()) {
            type = 3;
            show.setText(String.valueOf(type));
        } else if (vx_box.isChecked()) {
            type = 2;
            show.setText(String.valueOf(type));
        } else {
            type = 1;
            show.setText(String.valueOf(type));
        }
    }

    public void isSupportWYuE(boolean isVisible) {
        balance.setVisibility(isVisible ? View.VISIBLE : View.GONE);
        tv_other.setVisibility(isVisible ? View.VISIBLE : View.GONE);
    }


    private void dismiss(final Context context) {
        final CenterDialog centerDialog = new CenterDialog(context, R.layout.dialog_center);
        centerDialog.show();
        centerDialog.setCancelable(false);
        TextView textView = centerDialog.findViewById(R.id.dialog_text);
        textView.setText("是否跳转到我的订单");
        TextView textView1 = centerDialog.findViewById(R.id.dialog_sure);
        textView1.setTextColor(ContextCompat.getColor(context, R.color.red));
        textView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent0 = new Intent(context, MainActivity.class);
                intent0.putExtra("position", 3);
                Intent intent1 = new Intent(context, MyOrderActivity.class);
                intent1.putExtra("position", 0);
                Intent[] intents = new Intent[]{intent0, intent1};
                context.startActivities(intents);
            }
        });
        TextView textView2 = centerDialog.findViewById(R.id.dialog_cancel);
        textView2.setText("退出");
        textView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                centerDialog.dismiss();
            }
        });
    }

    public Activity getActivity() {
        return activity;
    }

    @Override
    public void showToastMsg(String msg) {
        Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void dismissLoading() {

    }
}
